package com.jinglvhuanjing.app.utils;

public class ByteUtil {
    private static char[] hexArray = "0123456789ABCDEF".toCharArray();

    /**
     * 字节数组转16进制字符串
     *
     * @param hexBytes 数组
     * @return 字符串
     */
    public static String hexBytesToString(byte[] hexBytes) {
        char[] hexChars = new char[hexBytes.length * 2];
        for (int j = 0; j < hexBytes.length; j++) {
            int v = hexBytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 字节数转16进制字符串
     *
     * @param hexByte 单字节
     * @return 字符串
     */
    public static String hexByteToString(byte hexByte) {
        char[] hexChars = new char[2];
            int v = hexByte & 0xFF;
            hexChars[0] = hexArray[v >>> 4];
            hexChars[1] = hexArray[v & 0x0F];
        return new String(hexChars);
    }

    /**
     * 16进制字符串转int
     *
     * @param hexString 字符串
     * @return int
     */
    public static int hexStringToInt(String hexString) {
        return Integer.parseInt(hexString, 16);
    }

    /**
     * 16进制字符串转byte数组
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * char转换位byte
     *
     * @param c char
     * @return byte
     */
    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 16进制字符串转二进制字符串
     * @param hex
     * @return
     */
    public static String hexStringToBinary(String hex) {
        String dfd = Long.toString(Long.parseLong(hex,16) , 2).toUpperCase();
        int qy = dfd.length()%4;
        if(qy!=0){
            return String_0(dfd, dfd.length()+(4-qy));
        }
        return dfd;
    }

    /**
     * 二进制字符串转十进制
     * @param binary
     * @return
     */
    public static int binaryToInt(String binary) {
        return Integer.parseInt(binary, 2);
    }

    /**
     * 二进制(4的倍数位数二进制)转十六进制
     * @param binary
     */
    public static String binaryToHex(String binary){
        StringBuffer hex = new StringBuffer();
        int iTmp;
        for(int i=0; i<binary.length(); i+=4){
            iTmp = 0;
            for(int j=0; j<4; j++){
                iTmp += Integer.parseInt(binary.substring(i+j, i+j+1))<<(4-j-1);
            }
            hex.append(Integer.toHexString(iTmp));
        }
        return hex.toString().toUpperCase();
    }

    /**
     * 十进制（数字）转十六进制
     * @param int_
     * @return
     */
    public static String intToHex(int int_){
        return Integer.toString(int_,16).toUpperCase();
    }


    /**
     * 16进制转Ascii
     * @param hexStr
     * @return
     */
    public static String hexToAscii(String hexStr) {
        StringBuilder output = new StringBuilder("");
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

    /**
     * 不足位数，前面补充0
     * @param bw
     * @returnString_0
     */
    public static String String_0(String bw, int length){
        if(bw.length()==length){
            return bw;
        }
        for(int i=0; i<=length-bw.length();i++){
            bw = "0"+bw;
        }
        return bw;
    }

    /**
     *
     * 0110 0000 0000 0000
     * 十六进制转二进制与算法
     * @param hex       16进制 的int类型 Integer.valueOf("hex", 16)
     * @param rightNum  右移位数
     * @param hex_      保留截至位数 的int类型 Integer.valueOf("hex_", 16)
     * @return
     */
    public static int binaryYU(int hex, int rightNum, int hex_){
        return (hex>>rightNum)&hex_;
    }

}
